// pages/transportPlan/transportPlan.ts
var url = require("../../utils/urls");
var commons = require("../../utils/common");
var tip = require("../../utils/tip");
var modelData = require("../../utils/data");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    automatic:'',
    planNumList:[],
    showModal:false,
    navList: ['固体', '液体', '气体'],
    nav_type: 0,
    checkedList: [], // planId,num
    list: [],
    // background: ['http://mtl-app.oss-cn-beijing.aliyuncs.com/upload/20221109/c190cf0161471594b5d0e8ad8967679b.webp'],
    bannerData: [],
    indicatorDots: true,
    vertical: false,
    autoplay: true,
    interval: 2000,
    duration: 500,
    show: false
  },
  changeType: function (e) {
    let {
      index
    } = e.currentTarget.dataset;
    if (this.data.nav_type=== index || index === undefined) {
      return false;
    } else {
      console.log(index,'index')
      this.setData({
        nav_type: index
      })
      this.getDetailsInTransit()
    }
  },
  goWebView(e) {
    let data = e.currentTarget.dataset
    if (data.externalLinks) {
      wx.navigateTo({
        url: "../webView/webView?externalLinks=" + data.externalLinks
      })
    }
  },
  checkboxChange(e) {
    const len = e.detail.value
    if(len.length > 5){
      console.log('不能大于5单')
    }else{
      let arr = e.detail.value
      var result = [];
      for (var i = 0; i < arr.length; i++) {
        var parts = arr[i].split(",");
        var namedObj = {
          id: parts[0],
          planNum: parts[1],
          syNum: parts[2]
        };
        result.push(namedObj);
      }
      console.log(result);
   
      this.setData({
        planNumList:result,
      });
    }
  },
  inputChangeP: function(e) {
    var index = e.currentTarget.dataset.index;
    var value = e.detail.value;
    console.log(index,'index')
    console.log(value,'value')  
    this.setData({
      [`planNumList[${index}].value`] : value,
    });
    console.log(this.data.planNumList,'this.data.planNumList')
  },
  goDispatch() {
    if(this.data.automatic){
      let arr = this.data.planNumList;
      let newArr = []
      arr.map((res)=>{
        newArr.push({
          planId:res.id,
          num:-1
        })
      })
      console.log(newArr,'newArr')
      wx.navigateTo({
        url: "../dispatch/dispatch?array=" + JSON.stringify(newArr),
      })
    }else{
      this.setData({
        showModal: true
      });
    }
  },
  hideModal: function() {
    this.setData({
      showModal: false
    });
  },
  confirm: function() {
    let arr = this.data.planNumList;
    let newArr = []
    arr.map((res)=>{
      newArr.push({
        planId:res.id,
        num:res.value
      })
    })
    console.log(newArr,'newArr')
    wx.navigateTo({
      url: "../dispatch/dispatch?array=" + JSON.stringify(newArr),
    })
    this.hideModal();
  },
  cancel: function() {
    console.log('用户点击取消');
    // 处理取消逻辑
    this.hideModal();
  },
  getBannerDetail: async function () {
    //planType  0:未完成计划 1：已完成计划  
    let requestUrl = url.getBannerDetail + "?type=1"
    let data = await commons.get(requestUrl);
    if (data.code == 200) {
      this.setData({
        bannerData: data.data
      })
      // console.log(this.data.bannerData, 'bannerData')
    } else {
      tip.showToast(data.msg, 'error', 1000)
    }
  },
  getDetailsInTransit: async function () {
    //planType  0:未完成计划 1：已完成计划  
    // let requestUrl = url.fleetLeaderPlan + "?planType=0"
    let requestUrl = url.hazardousGoodsRemainingPlans + "?type=1&goodsStatus="+this.data.nav_type
    let data = await commons.postJson(requestUrl);
    if (data.code == 200) {
      let resData = data.data
      resData.forEach((item, index) => {
        // item.vehicleFont = commons.getFont(modelData.vehicleType, item.vehicleType)
        item.transportedCount = commons.NumberSub(item.goodsPreviousCount, item.goodsSurplusCount)
        resData.splice(index, 1, item)
      });
      this.setData({
        list: resData,
        show: true,
      })
    } else {
      tip.showToast(data.msg, 'error', 1000)
    }
  },
 //查看详情
  goTransportPlanDetail(e) {
    console.log(1111)
    console.log('xiangqing')
    let item = e.currentTarget.dataset.item
    wx.navigateTo({
      url: "../transportPlanDetail/transportPlanDetail?item=" + JSON.stringify(item),
    })
  },
  //分享计划码
  shareView: function (e) {
    let planQrCode = e.currentTarget.dataset.item.planQrCode;
    planQrCode = planQrCode.replace('http', 'https');
    // let planQrCode ='http://mtl-app.oss-cn-beijing.aliyuncs.com/upload/20230524/59bb47593c2dd4122b0971b9e8fa94cd.png'
    wx.downloadFile({
      url: planQrCode,
      success: (res) => {
        console.log(res, 'resresresres');
        wx.showShareImageMenu({
          path: res.tempFilePath
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(e) {
    console.log(e.automatic,'automatic')
    if(e.automatic){
       this.data.automatic = e.automatic
    }
    let userInfo = wx.getStorageSync("userInfo");
    if (userInfo.roleAlias == 'boss') {
      wx.redirectTo(
        {
          url: "../management/management",
          fail(err) { console.log(err); }
        })
      return;
    }
    wx.showShareMenu({
      withShareTicket: true
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getBannerDetail()
    this.getDetailsInTransit()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.getDetailsInTransit()
    setTimeout(() => {
      wx.stopPullDownRefresh()
    }, 1000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {




  }
})
